<?php

class UploadController extends Controller
{
	public function actionIndex()
	{
		$model=new Video;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Video']))
		{
			$model->attributes=$_POST['Video'];
            $model->video=CUploadedFile::getInstance($model,'video');
            
			if($model->save()) {
                $model->video->saveAs('/var/www/xemhomnay/trunk/data/test.avi');

                require_once('/var/www/xemhomnay/trunk/fo/protected/extensions/ZendGdata/library/Zend/Loader.php');
                Zend_Loader::loadClass('Zend_Gdata_YouTube');
                Zend_Loader::loadClass('Zend_Gdata_AuthSub');
                Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 
                
                $this->getAndPrintUserUploads('ntdoan.vn@gmail.com');
                die;
//                $developerKey = Yii::app()->params['YouTube-API'];
//                $applicationId = 'Video uploader v1';
//                $clientId = 'My video upload client - v1';
//
//                $yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
//
//                $authenticationURL= 'https://www.google.com/accounts/ClientLogin';
//                $httpClient = 
//                  Zend_Gdata_ClientLogin::getHttpClient(
//                  $username = 'ntdoan.vn@gmail.com',
//                  $password = 'H27zOP N9B',
//                  $service = 'youtube',
//                  $client = null,
//                  $source = 'MySource', // a short string identifying your application
//                  $loginToken = null,
//                  $loginCaptcha = null,
//                  $authenticationURL);

				$this->redirect(array('view','id'=>$model->id));
            }
		}

		$this->render('index',array(
			'model'=>$model,
		));
	}

    function getAndPrintUserUploads($userName)                    
    {     
      $yt = new Zend_Gdata_YouTube();
      $yt->setMajorProtocolVersion(2);
      printVideoFeed($yt->getuserUploads($userName));
    } 
    function getAuthSubRequestUrl()
    {
        $next = 'http://www.example.com/welcome.php';
        $scope = 'http://gdata.youtube.com';
        $secure = false;
        $session = true;
        return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session);
    }
    
    function getAuthSubHttpClient()
    {
        if (!isset($_SESSION['sessionToken']) && !isset($_GET['token']) ){
            echo '<a href="' . getAuthSubRequestUrl() . '">Login!</a>';
            return;
        } else if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
          $_SESSION['sessionToken'] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
        }

        $httpClient = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']);
        return $httpClient;
    }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}